/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.sel1602.csp.Dao.impl;

import edu.fjut.sel1602.csp.Dao.PetDao;
import edu.fjut.sel1602.csp.entity.Pet;
import edu.fjut.sel1602.csp.util.BaseDao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lenovo
 */
public class PetDaoOracleImpl implements PetDao{
    @Override
    public int add(Pet pet) {
        int r=0;
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement pst=con.prepareStatement("insert into pet(NAME,TYPENAME,HEALTH,LOVE,BIRTHDAY,PRICE) values(?,?,?,?,?,?)");
            pst.setString(1, pet.getName());
            pst.setString(2, pet.getTypename());
            pst.setInt(3, pet.getHealth());
            pst.setInt(4, pet.getLove());
            pst.setDate(5, (Date) pet.getBirthday());
            pst.setInt(6, pet.getPrice());
            r=pst.executeUpdate();
            BaseDao.closeConnection(con); 
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public List getPetName() {
        List<String> list=new ArrayList<String>();
        try {
            Connection con=BaseDao.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select NAME from pet");
            while(rs.next())
            {
                String name=rs.getString("NAME");
                list.add(name);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return list;
    }

    @Override
    //显示宠物表所有信息
    public List<Pet> getAllPet() {
        List<Pet> list=new ArrayList<Pet>();
        try {
            Connection con=BaseDao.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select * from pet");
            while(rs.next())
            {
                Pet pet=new Pet();
                pet.setId(rs.getInt("ID"));
                pet.setName(rs.getString("NAME"));
                pet.setTypename(rs.getString("TYPENAME"));
                pet.setHealth(rs.getInt("HEALTH"));
                pet.setLove(rs.getInt("LOVE"));
                pet.setStoreid(rs.getInt("STORE_ID"));
                pet.setOwnerid(rs.getInt("OWNER_ID"));
                pet.setBirthday(rs.getDate("BIRTHDAY"));
                pet.setPrice(rs.getInt("PRICE"));
                list.add(pet);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return list;
    }

    //@Override
    //显示表的库存宠物
    /*public List<Pet> getAccountPet() {
         List<Pet> list=new ArrayList<Pet>();
        try {
            Connection con=BaseDao.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select a.ID,a.NAME,a.TYPENAME,a.HEALTH,a.LOVE,a.BIRTHDAY,a.PRICE from pet a,accont b where a.ID=b.PET_ID");
            while(rs.next())
            {
                Pet pet=new Pet();
                pet.setId(rs.getInt("ID"));
                pet.setName(rs.getString("NAME"));
                pet.setTypename(rs.getString("TYPENAME"));
                pet.setHealth(rs.getInt("HEALTH"));
                pet.setLove(rs.getInt("LOVE"));
                pet.setStoreid(rs.getInt("STORE_ID"));
                pet.setOwnerid(rs.getInt("OWNER_ID"));
                pet.setBirthday(rs.getDate("BIRTHDAY"));
                pet.setPrice(rs.getInt("PRICE"));
                list.add(pet);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    @Override
    public List<Pet> getOwnerPet(int ownerid) {
        try {
            List<Pet> list=new ArrayList<Pet>();
            String sql="select PET.ID,pet.NAME,TYPENAME from pet,petowner where pet.OWNER_ID=petowner.ID and petowner.ID=?";
            Object[] objs={ownerid};
            ResultSet rs =bd.executeQuery(sql,objs);
            if(rs!=null){
                while(rs.next()){
                    Pet pet=new Pet();
                    pet.setId(rs.getInt("ID"));
                    pet.setName(rs.getString("NAME"));
                    pet.setTypename(rs.getString("TYPENAME"));
                    list.add(pet);
                }
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
 }

    @Override
    public Pet getPetById(int id) {
        try {
            String sql="select NAME,TYPENAME from pet where ID=?";
            Object[] objs={id};
            ResultSet rs = bd.executeQuery(sql, objs);
            Pet pet=new Pet();
            if(rs.next()){
                pet.setName(rs.getString("NAME"));
                pet.setTypename(rs.getString("TYPENAME"));
                System.out.println("宠物名字叫:"+pet.getName()+"\t宠物类别是："+pet.getTypename());
            }
            return pet;
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
	}*/

    

	
	
}

    
    
    

